#!/bin/sh
#grep -q '2\.6\.9' /proc/version || exit 0 
# chkconfig:    345 91 67
# description:  Starts and stops MesgSrv daemon.

# Source function library
. /etc/rc.d/init.d/functions

# the configuration file
if [ -f /etc/sysconfig/msgsrv ]
	then
		. /etc/sysconfig/msgsrv
	else
		echo "MsgSrv: configuration file not found."
		exit 1
	fi

start_srv()
{
	msg_starting "$1"
	$*
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		[ -s "/var/run/$1.pid" ] || {
			fail
			exit 1
		}
		ok
	else
		fail
	fi
}  

stop_srv()
{
	msg_stopping "$1"
	if [ -s "/var/run/$1.pid" ]; then
		TMP="`cat /var/run/$1.pid`"
		kill -s HUP $TMP
		sleep 1
		[ -d "/proc/$TMP" ] && {
			fail
			exit 0
		}
		ok
	else
		died
	fi
}

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/msgsrv ]; then
	touch /var/lock/subsys/msgsrv
	is_yes "$START_SERVER" && start_srv msgsrv
	if [ -n "$START_CLIENTS" ]; then
		for cli in $START_CLIENTS; do
			start_srv $cli
		done
	fi
	else
		msg_already_running "MsgSrv"
		exit 1
	fi
        ;;
  stop)
	if [ -f /var/lock/subsys/msgsrv ]; then
		rm -f /var/lock/subsys/msgsrv >/dev/null 2>&1
		if [ -n "$START_CLIENTS" ]; then
			for cli in $START_CLIENTS; do
				stop_srv $cli
			done
		fi
		is_yes "$START_SERVER" && stop_srv msgsrv
	else
		msg_not_running "MsgSrv"
		exit 1
	fi
        ;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
        msg_usage "$0 {start|stop|restart|reload}"
        exit 1
esac

exit $RETVAL
